package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.IntegerMallBean;
import com.dingwei.superiordiary.bean.OrderListBean;
import com.dingwei.superiordiary.bean.OrderPayBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class MyOrderPresenter extends basePresenter {

    private Contract.MyOrderView myOrderView;

    public MyOrderPresenter(Contract.MyOrderView view) {
        super(view);
        this.myOrderView = view;
    }


    public void initData(int page, final boolean isRefresh, boolean isShowDialog,String status) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.ORDER_LIST);
        rp.addBodyParameter("page",page+"");
        rp.addBodyParameter("status",status+"");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());


        x.http().post(rp, new myCallback(BaseActivity.context,OrderListBean.class,isShowDialog,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                OrderListBean bean = (OrderListBean) baseBean;
                if (isRefresh){
                    myOrderView.onRefresh(bean);
                }else {
                    myOrderView.onLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myOrderView.onFail();
            }
        });

    }

    public void orderConfrm(String id, final int position) {

        RequestParams rp = new RequestParams(URLS.HOST+URLS.ORDER_CONFIRM);
        rp.addBodyParameter("id",id+"");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());


        x.http().post(rp, new myCallback(BaseActivity.context,OrderListBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                myOrderView.orderConfirmSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });

    }
    public void deleteOrder(String id, final int position) {

        RequestParams rp = new RequestParams(URLS.HOST+URLS.DELETE_ORDER);
        rp.addBodyParameter("id",id+"");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());


        x.http().post(rp, new myCallback(BaseActivity.context,OrderListBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                myOrderView.deleteOrderSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });

    }

    public void payOrder(String id, final String payTag) {

        RequestParams rp = new RequestParams(URLS.HOST+URLS.PAY_ORDER1);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        rp.addBodyParameter("id",id);
        x.http().post(rp, new myCallback(BaseActivity.context, OrderPayBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                OrderPayBean bean = (OrderPayBean) baseBean;
                if (payTag.equals("wx")){
                    myOrderView.wxPay(bean);
                }else if (payTag.equals("alipay")){
                    myOrderView.alipay(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
